package com.example.shiro.core.exception;

import com.example.shiro.util.ApiResult;
import com.example.shiro.util.ResultGenerator;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 全局异常类定义
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ApiResult handler(Exception e) {
         if (e instanceof AuthorizationException) {
             logger.error("没有权限....", e);
             return ResultGenerator.getUnauthorizedResult();
        } else {
             logger.error("系统异常....", e);
             return ResultGenerator.getFailResult("系统异常");
        }
    }
}